package Thread.ThreadSyncMethod;

public class ThreadSyncMethod {
    public static void main(String[] args) throws Exception {
        var c1 = new Counter();
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 1000; i++) {
                c1.add(1);
            }
        });
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 1000; i++) {
                c1.dec(1);
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(c1.get());
        System.out.println(c1.get());
        System.out.println(c1.get());
        System.out.println(c1.get());
    }
}

class Counter {
    private int count = 0;

    public void add(int n) {
        synchronized (this) {
            count += n;
        }
    }

    public void dec(int n) {
        synchronized (this) {
            count -= n;
        }
    }

    public synchronized int get() {
        return count;
    }

}
